#include"starhzk.h"

int hzprintf(HWND hwnd,char *p)
{
    if(hztype==16)
        puthz16(hwnd,hz16,hzx,hzy,hzz,hzcolorR,hzcolorG,hzcolorB,hzm,hzn,p);
    if(hztype==24)
        puthz24(hz24,hzx,hzy,hzz,hzcolorR,hzm,hzn,p);
}
int hz_type(int type)
{
    hztype=type;
}

int hz_lib(int ziti)
{
    switch(ziti)
    {
        case HZK_ABC:
        hz16=HZK_ABC;
        break;
        case HZK_ABCF:
        hz16=HZK_ABCF;
        break;
        case KAI:
        hz24=KAI;
        break;
        case HEI:
        hz24=HEI;
        break;
        case SONG:
        hz24=SONG;
        break;
        case FANG:
        hz24=FANG;
        break;
    }
}
int hz_xy(int x, int y)
{
    hzx=x;
    hzy=y;
}
int hzxy(int x,int y)
{
    hzx=x*8;
    hzy=y*16;
}
int hz_z(int z)
{
    hzz=z;
}
int hz_mn(int m,int n)
{
    hzm=m;
    hzn=n;
}
int hz_color(int colorR,int colorG,int colorB)
{
    hzcolorR=colorR;
    hzcolorG=colorG;
    hzcolorB=colorB;
}

int puthz16(HWND hwnd,
            int hz,                             //font
            int x,int y,                        //position
            int z,                              //space-width
            int colorR,int colorG,int colorB,   //color
            int m,int n,                        //scale
            char *p)                            //text
{
    unsigned int i,c1,c2,f=0;
    int i1,i2,i3,i4,i5,rec;
    int handle;
    long l;
    char by[32];
    HPEN hpen;
    HDC hdc;

    hpen = CreatePen(PS_SOLID, 1 ,RGB(colorR,colorG,colorB));
    hdc = GetDC(hwnd);
    SelectObject(hdc, hpen);
    handle = openhzk(hz);
    while((i=*p++)!=0)
    {
        if(i>0xa1)
            if(f==0)
            {
                c1=(i-0xa1)&0x07f;
                f=1;
            }
            else
            {
                c2=(i-0xa1)&0x07f;
                f=0;
                rec=c1*94+c2;
                l=rec*32L;
                lseek(handle,l,SEEK_SET);
                read(handle,by,32);
                for(i1=0;i1<16*n;i1+=n)
                    for(i4=0;i4<n;i4++)
                        for(i2=0;i2<2;i2++)
                            for(i3=0;i3<8;i3++)
                                if(getbit(by[i1/n*2+i2],7-i3))
                                    for(i5=0;i5<m;i5++)
                                    {
                                        MoveToEx(hdc,x+i2*8*m+i3*m+i5-1,y+i1+i4-1,NULL);
                                        LineTo(hdc,x+i2*8*m+i3*m+i5,y+i1+i4);
                                    }
                x+=16*m+z;
            }
    }
    ReleaseDC(hwnd,hdc);
    DeleteObject(hpen);
    closehzk(handle);
    return(x);
}

int puthz24(int hzk,int x,int y,int z,int color,int m,int n, char *p)
{
    unsigned int i,c1,c2,f=0;
    int i1,i2,i3,i4,i5,rec;
    long l;
    char by[72];
    int handle;

    handle=openhzk(hzk);
    while((i=*p++)!=0)
    {
        if(i>0xa1)
            if(f==0)
            {
                c1=(i-0xa1)&0x07f;
                f=1;
            }
            else
            {
                c2=(i-0xa1)&0x07f;
                f=0;
                rec=(c1-15)*94+c2;
                l=rec*72L;
                lseek(handle,l,SEEK_SET);
                read(handle,by,72);
                for(i1=0;i1<24*m;i1+=m)
                    for(i4=0;i4<m;i4++)
                        for(i2=0;i2<=2;i2++)
                            for(i3=0;i3<8;i3++)
                                if(getbit(by[i1/m*3+i2],7-i3))
                                    for(i5=0;i5<n;i5++)
                                        ;//putpixel(x+i1+i4,y+i2*8*n+i3*n+i5,color);
                x+=24*m+z;
            }
    }
    closehzk(handle);
    return(x);
}
int getbit(unsigned char c,int n)
{
    return((c>>n)&1);
}
int openhzk(int c)
{
    int handle;
    if(c==1)
        handle=open(HZK_PATH"hzk24s",O_RDONLY|O_BINARY);
    if(c==2)
        handle=open(HZK_PATH"hzk24f",O_RDONLY|O_BINARY);
    if(c==3)
        handle=open(HZK_PATH"hzk24k",O_RDONLY|O_BINARY);
    if(c==4)
        handle=open(HZK_PATH"hzk24h",O_RDONLY|O_BINARY);
    if(c==5)
        handle=open(HZK_PATH"hzk24t",O_RDONLY|O_BINARY);
    if(c==0)
        handle=open(HZ_PATH"hzk16",O_RDONLY|O_BINARY);
    if(c==-1)
        handle=open(HZ_PATH"hzk16f",O_RDONLY|O_BINARY);
    if(handle==-1)
    {
        printf("Cannot open the HZK*.*");
        getchar();
        exit(-1);
    }
    return handle;
}
int closehzk(int handle)
{
    close(handle);
    return(0);
}

/*int putzf24(int hzk,int x,int y,int z,int color,int m,int n, char *p)
{ unsigned int i,c1,c2,f=0;
int i1,i2,i3,i4,i5,rec;
long l;
char by[72];
openhzk(TE);
while((i=*p++)!=0)
 {if(i>0xa1)
  if(f==0)
   {c1=(i-0xa1)&0x07f;
   f=1;
   }
  else
   {c2=(i-0xa1)&0x07f;
   f=0;
   rec=(c1-15)*94+c2;
   l=rec*72L;
   lseek(handle,l,SEEK_SET);
   read(handle,by,72);
   for(i1=0;i1<24*m;i1+=m)
    for(i4=0;i4<m;i4++)
     for(i2=0;i2<=2;i2++)
      for(i3=0;i3<8;i3++)
       if(getbit(by[i1/m*3+i2],7-i3))
        for(i5=0;i5<n;i5++)
         putpixel(x+i1+i4,y+i2*8*n+i3*n+i5,color);
   x+=24*m+z;
   }
 }
closehzk();
return(x);
}
*/
